/**
 * Copyright (c) 2011-2012 Nokia Corporation.
 */

import QtQuick 1.1

Image {
    id: slider
    smooth: true
    width: parent.width - 40; x: 20;
    fillMode: Image.Stretch
    property real value: 0.5
    property real minval: 0
    property real maxval: 1
    property string label: "no name"
    property int xMax: width - handle.width
    onValueChanged: updatePos();
    onXMaxChanged: updatePos();
    onMinvalChanged: updatePos();

    function updatePos() {
        if (maxval > minval) {
            var pos = (value - minval) * slider.xMax / (maxval - minval);
            pos = Math.min(pos, width - handle.width);
            pos = Math.max(pos, 0);
            handle.x = pos;
        } else {
            var pos = (value - maxval) * slider.xMax / (minval - maxval);
            pos = Math.min(pos, width - handle.width);
            pos = Math.max(pos, 0);
            handle.x = width - handle.width - pos;
        }
    }

    Image {
        id: handle;
        source: "qrc:/gfx/slider.png"
        smooth: true
        height: slider.height / 1.8;
        width: slider.width / 6.0;
        y: slider.height / 1.8;
        fillMode: Image.Stretch

        MouseArea {
            id: mouse
            anchors.fill: parent
            drag.target: parent
            drag.axis: Drag.XAxis
            drag.minimumX: 0
            drag.maximumX: slider.xMax

            onPositionChanged: {
                slider.value = (slider.maxval - slider.minval) * handle.x /
                    slider.xMax + slider.minval;
            }
        }
    }
}
